﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MinimizeEngine.Components.Render.Grid_System;
using MinimizeEngine.Managers;

namespace MinimizeEngine.Entities.Grid_System
{
    public class MEGridSystem : MEBaseEntity
    {
        #region Fields

        /// <summary>
        /// Number of Rows in the Grid System
        /// </summary>
        private int mRows;

        /// <summary>
        /// Number of Columns in the Grid System
        /// </summary>
        private int mColumns;

        /// <summary>
        /// Width of each Grid Section
        /// </summary>
        private float mGSWidth;

        /// <summary>
        /// Height of each Grid Section
        /// </summary>
        private float mGSHeight;

        /// <summary>
        /// Padding between each Grid Section
        /// </summary>
        private float mPadding;

        /// <summary>
        /// Name of the Texture to be used for this Grid System
        /// </summary>
        private string mTextureName;

        #endregion

        #region Properties

        /// <summary>
        /// Number of Rows in the Grid System
        /// </summary>
        public int Rows
        {
            get { return mRows; }
            set { mRows = value; }
        }

        /// <summary>
        /// Number of Columns in the Grid System
        /// </summary>
        public int Columns
        {
            get { return mColumns; }
            set { mColumns = value; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Creates a new Grid System
        /// </summary>
        /// <param name="name">Name of the Grid System</param>
        /// <param name="position">Position of the Grid System</param>
        /// <param name="rotation">Rotation of the Grid System</param>
        /// <param name="rows">How many Rows in the Grid System</param>
        /// <param name="cols">How many Columns in the Grid System</param>
        /// <param name="padding">Padding between each Grid Section</param>
        public MEGridSystem(string name, Vector3 position, Matrix rotation, int rows, int cols, float padding, float gridSectionWidth, float gridSectionHeight, string texturePath) : base(name, position, rotation)
        {
            mRows = rows;
            mColumns = cols;
            mGSWidth = gridSectionWidth;
            mGSHeight = gridSectionHeight;
            mPadding = padding;

            METextureManager.AddTexture2D(name + "_Texture", texturePath);
            mTextureName = name + "_Texture";

            InitializeGridSections();
        }

        private void InitializeGridSections()
        {
            float height = 0;
            float width = 0;
            float widthPadding = 0;
            float heightPadding = 0;
            MEGridSection gridSection;
            MEGridSectionRenderComponent gridSectionRenderComponent;
            BasicEffect mBasicEffect = new BasicEffect(MEEngineManager.DeviceManager.GraphicsDevice, null);
            mBasicEffect.TextureEnabled = true;
            mBasicEffect.Texture = METextureManager.GetTexture2D(mTextureName);

            for (int row = 0; row < mRows; row++, height += mGSHeight, heightPadding += mPadding)
            {
                for (int col = 0; col < mColumns; col++, width += mGSWidth, widthPadding += mPadding)
                {
                    Vector3 pos = Position + new Vector3(width + widthPadding, 0, height + heightPadding);
                    gridSection = new MEGridSection(this.Name + "_" + row + "_" + col, pos, new Vector2(row, col), mGSWidth, mGSHeight, Rotation);
                    gridSectionRenderComponent = new MEGridSectionRenderComponent(gridSection, mTextureName, Vector3.Backward, Vector3.Up);
                    gridSectionRenderComponent.BasicEffect = mBasicEffect;

                    //Add to Master Lists
                    AddChildEntity(gridSection);
                    AddComponent(gridSectionRenderComponent);
                }
                width = 0;
                widthPadding = 0;
            }
        }
        
        #endregion
    }
}
